import crypto from 'crypto'


/* md5加密 */
export function md5(value) {
  const md5 = crypto.createHash('md5');
  md5.update(value);
  return md5.digest("hex")
}

export function startLoading({app,text = 'Loading',spinner = 'el-icon-loading',background = 'rgba(0, 0, 0, 0.7)'}) {
  const loading = app.$loading({
    lock: app,
    text: text,
    spinner: spinner,
    background: background
  });
  return loading
}

export function getImagePath(path) {
  return process.env.IMAGE_PATH + path
}

export function downloadIamge(imgsrc, name) {//下载图片地址和图片名
  let image = new Image();
  image.setAttribute("crossOrigin", "anonymous");
  image.onload = function() {
    let canvas = document.createElement("canvas");
    canvas.width = image.width;
    canvas.height = image.height;
    let context = canvas.getContext("2d");
    context.drawImage(image, 0, 0, image.width, image.height);
    let url = canvas.toDataURL("image/png"); //得到图片的base64编码数据
    let a = document.createElement("a"); // 生成一个a元素
    let event = new MouseEvent("click"); // 创建一个单击事件
    a.download = name || "photo"; // 设置图片名称
    a.href = url; // 将生成的URL设置为a.href属性
    a.dispatchEvent(event); // 触发a的单击事件
  };
  image.src = imgsrc;
}

export function isPhone(rule, value, callback){
    if (!value) {
        return callback(new Error('请输入手机号'));
    } else {
        const reg = /^1[3|4|5|7|8|9][0-9]\d{8}$/
        if (reg.test(value)) {
           return callback();
        } else {
            return callback(new Error('请输入正确的手机号'));
        }
    }
}
